package com.xn.monitor;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by yangshoujun on 2017/7/17 下午3:50.
 */
public class MonitorDemo {

    private static boolean RUNNING = true;

    public static void main(String[] args) {

        final AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        context.registerShutdownHook();

        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

            @Override
            public void run() {
                context.stop();
                context.close();
                synchronized (MonitorDemo.class) {
                    MonitorDemo.RUNNING = false;
                    MonitorDemo.class.notify();
                }
            }
        }));
        System.out.println("started...");

        synchronized (MonitorDemo.class) {
            if(RUNNING) {
                try {
                    MonitorDemo.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("stoped...");
    }

}
